/**
 * 楼盘信息
 */
define(function (require, exports, module) {
    var angular = require('angular');
    var util = require("util");
    var ui = require('ui');
    var api_prefix = G_CONFIG["api_prefix"] + "/project/project/"; //api前缀
    //汉字长度指令
    var dblengthDirective=function () {
        function getLength(min, max) {
            return function (val) {
                var l = !val ? 0 : (function len(s) {
                    var l = 0;
                    var a = s.split("");
                    for (var i = 0; i < a.length; i++) {
                        if (a[i].charCodeAt(0) < 299) {
                            l++;
                        } else {
                            l += 2;
                        }
                    }
                    return l;
                })(val);
                return l >= min && l <= max;
            };
        }

        return {
            require: 'ngModel',
            link: function (scope, iElement, iAttrs, ctrl) {
                var ml = scope.$eval(iElement.attr("dblength"));
                var min = 0;
                var max = parseInt(ml) || 255;
                var validate = getLength(min, max);
                ctrl.$validators.validateDblength = function(modelValue, viewValue) {
                    var value = modelValue || viewValue;
                    var ret = validate(value);
                    ctrl.$setValidity('maxdblength', ret);
                    return ret;
                };
                //ctrl.$parsers.push(function (val) {
                //    ctrl.$setValidity('maxdblength', validate(val));
                //    return val;
                //});
            }
        };
    };
    angular.module('buildingsDetail',[])
    .directive("dblength", dblengthDirective)
    .factory('buildingsDetailService',['$http',function($http){
        return {
            getDetail : function(curProjectId,scope){
                var url = api_prefix + 'get-building-info-by-project-id'
                    ,data = {
                        projectId : curProjectId,
                        NEED_TOKEN: false
                    };
                $http.get(url,{params:data}).then(function (res) {
                    if (res.data.retCode == 0) {
                        scope.building = res.data.data || {};
                        if(scope.building.open_date){
                            scope.building.open_date = util.formatDate(util.parseDate(scope.building.open_date),'yyyy-MM-dd')
                        }

                        var comAddress = '';
                        if (scope.building.provinceName) {
                            comAddress += scope.building.provinceName;
                        }
                        if (scope.building.cityName) {
                            comAddress += "-" + scope.building.cityName;
                        }
                        if (scope.building.areaName) {
                            comAddress += "-" + scope.building.areaName;
                        }
                        scope.building.com_address = comAddress;
                    } else {
                        ui.showErrTip(res.data.errMsg);
                    }
                }).finally(function(){
                    scope.loading = false;
                });
            },
            save : function(scope){
                var url = api_prefix + 'save-building-info'
                    ,data = angular.copy(scope.building);
                data.NEED_TOKEN = false;
                $http.post(url, data).then(function (res) {
                    if (res.data.retCode == 0) {
                        util.notify('保存成功！');
                    } else {
                        ui.showErrTip(res.data.errMsg);
                    }
                });
            }
        }
    }])
    .controller('buildingsDetailCtl',['$rootScope','$scope','buildingsDetailService',function($rootScope,$scope,buildingsDetailService){
        $scope.building={};

        /**
         * 设置经纬度
         */
        $scope.selLocation=function(){
            ui.selLocation(function(data){
                $scope.$apply(function(){
                    $scope.building.latitude=data.latitude;
                    $scope.building.longitude=data.longitude;
                });
            },{latitude:$scope.building.latitude,longitude:$scope.building.longitude});
        };

        /**
         * 保存
         */
        $scope.save = function(){
            if ($scope.canEdit == 0) {
                return
            }
            $scope.buildingsForm.$setSubmitted(true);
            if($scope.buildingsForm.$valid){
                buildingsDetailService.save($scope);
            }else{
                $('input.ng-invalid').first().focus();
            }
        };

        /**
         *  项目切换时
          */
        $scope.$on('changeCurProjectId',function(event,curProjectId){
            $scope.curProjectId = curProjectId;
            $scope.initData();
        });

        $scope.$on('changeCanEdit', function (event, canEdit) {
            $scope.canEdit = canEdit;
        });

        /**
         * 初始化楼盘详情
         */
        $scope.initData = function(){

            buildingsDetailService.getDetail($rootScope.curProjectId,$scope);
        };

        //路由切换时
        if($rootScope.curProjectId){
            $scope.initData();
        }

    }]);
});